package com.loe.ktui.ui

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.ImageView
import com.loe.ktui.src
import com.loe.ktui.util.UiShape

open class UiImage(id: String? = null) : UiView(id)
{
    override var bg: Any? = Color.TRANSPARENT

    var rate: Double? = null
    var auto: Boolean? = null
    var colorFilter: Int? = null
    var scaleType: ImageView.ScaleType? = null
    var src: Any? = null

    open fun initUI(context: Context, view: KtImageView)
    {
        super.initUI(context, view)

        isClick?.let { view.setClick(it) }

        rate?.let { view.rate = it }
        auto?.let { view.auto = it }
        colorFilter?.let { view.setColorFilter(it) }
        scaleType?.let { view.scaleType = it }
        src?.let {
            if (it is UiShape)
            {
                view.setImageDrawable(it.createDrawable())
            } else
            {
                view.src = it.toString()
            }
        }
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtImageView
    {
        return KtImageView(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_Image(id: String? = null, block: (UiImage.() -> Unit)? = null): UiImage
{
    val ui = UiImage(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

@SuppressLint("AppCompatCustomView")
open class KtImageView : ImageView
{
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(attrs)
    }

    private fun initXML(attrs: AttributeSet)
    {
    }

    /******************************************** 原生 ******************************************/

    /** 固定比例 */
    var rate = -1.0

    /** 是否自动高度 */
    var auto = false

    var urlString = ""

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
    {
        if (drawable == null)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            return
        }
        // 计算出ImageView的宽度
        val viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        if (auto)
        {
            // 根据图片长宽比例算出ImageView的高度
            val viewHeight = viewWidth * drawable.intrinsicHeight / drawable.intrinsicWidth
            // 将计算出的宽度和高度设置为图片显示的大小
            setMeasuredDimension(viewWidth, viewHeight)
        } else if (rate > 0)
        {
            // 将计算出的宽度和高度设置为图片显示的大小
            setMeasuredDimension(viewWidth, (viewWidth * rate).toInt())
        } else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        }
    }

    /********************************************** 点击 ****************************************/

    fun setClick(isClick: Boolean)
    {
        isAlpha = isClick
        isClickable = isClick
    }

    private var isAlpha = false

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlpha) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlpha) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }
}